﻿#region zh-CHS 2006 - 2010 DemoSoft 团队 | en 2006-2010 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2010 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Mmose.Core.Common.SafeCollections;
#endregion

namespace Demo.Mmose.Core.Entity.Suit.Quest
{
    /// <summary>
    /// 任务事件
    /// </summary>
    public class QuestEvenHandler
    {
        #region zh-CHS 私有常量 | en Private Constants
        /// <summary>
        ///
        /// </summary>
        private const int DICTIONARY_CAPACITY = 1024;
        #endregion

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, SafeDictionary<Quest, object>> m_HighestQuestEvens = new SafeDictionary<long, SafeDictionary<Quest, object>>( DICTIONARY_CAPACITY );
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, SafeDictionary<Quest, object>> m_NormalQuestEvens = new SafeDictionary<long, SafeDictionary<Quest, object>>( DICTIONARY_CAPACITY );
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, SafeDictionary<Quest, object>> m_LowestQuestEvens = new SafeDictionary<long, SafeDictionary<Quest, object>>( DICTIONARY_CAPACITY );
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dddd"></param>
        public void LogIn<ObjectT, QuestT>( long questEvenId, QuestEvenPriority questEvenPriority, QuestT quest, QuestEvenCallback<ObjectT, QuestT> callBack ) where QuestT : Quest
        {
            SafeDictionary<long, SafeDictionary<Quest, object>> questEvenDictionary = null;
            switch ( questEvenPriority )
            {
                case QuestEvenPriority.Normal:

                    questEvenDictionary = m_NormalQuestEvens;
                    break;
                case QuestEvenPriority.Highest:

                    questEvenDictionary = m_HighestQuestEvens;
                    break;
                case QuestEvenPriority.Lowest:

                    questEvenDictionary = m_LowestQuestEvens;
                    break;
                default:

                    break;
            }

            if ( questEvenDictionary == null )
                return;

            SafeDictionary<Quest, object> questDictionary = null;
            SafeDictionary<Quest, object> addQuestDictionary = null;
            do
            {
                if ( questEvenDictionary.TryGetValue( questEvenId, out questDictionary ) == false )
                {
                    if ( addQuestDictionary == null )
                        addQuestDictionary = new SafeDictionary<Quest, object>( DICTIONARY_CAPACITY );

                    if ( questEvenDictionary.TryAdd( questEvenId, addQuestDictionary ) == true )
                    {
                        questDictionary = addQuestDictionary;
                        break;
                    }
                }
            } while ( questDictionary == null );

            QuestEven<ObjectT, QuestT> QuestEven = new QuestEven<ObjectT, QuestT>() { Quest = quest, Callback = callBack };

            if ( questDictionary != null )
                questDictionary.AddOrReplace( quest, QuestEven );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="ObjectT"></typeparam>
        /// <typeparam name="QuestT"></typeparam>
        /// <param name="questEvenId"></param>
        /// <param name="questObject"></param>
        /// <returns>回调了几次</returns>
        public int Callback<ObjectT, QuestT>( long questEvenId, ObjectT questObject ) where QuestT : Quest
        {
            int iCallbackCount = 0;

            // Callback 最高优先级
            SafeDictionary<Quest, object> questDictionary = null;
            if ( m_HighestQuestEvens.TryGetValue( questEvenId, out questDictionary ) == true )
            {
                if ( questDictionary != null )
                {
                    foreach ( var QuestEvenObject in questDictionary.ToArray() )
                    {
                        QuestEven<ObjectT, QuestT> QuestEven = QuestEvenObject.Value as QuestEven<ObjectT, QuestT>;
                        if ( QuestEven != null )
                        {
                            QuestEven.Object = questObject;
                            QuestEven.Callback( QuestEven );

                            iCallbackCount++;
                        }
                    }
                }
            }

            // Callback 普通优先级
            questDictionary = null;
            if ( m_NormalQuestEvens.TryGetValue( questEvenId, out questDictionary ) == true )
            {
                if ( questDictionary != null )
                {
                    foreach ( var QuestEvenObject in questDictionary.ToArray() )
                    {
                        QuestEven<ObjectT, QuestT> QuestEven = QuestEvenObject.Value as QuestEven<ObjectT, QuestT>;
                        if ( QuestEven != null )
                        {
                            QuestEven.Object = questObject;
                            QuestEven.Callback( QuestEven );

                            iCallbackCount++;
                        }
                    }
                }
            }

            // Callback 最低优先级
            questDictionary = null;
            if ( m_LowestQuestEvens.TryGetValue( questEvenId, out questDictionary ) == false )
            {
                if ( questDictionary != null )
                {
                    foreach ( var QuestEvenObject in questDictionary.ToArray() )
                    {
                        QuestEven<ObjectT, QuestT> QuestEven = QuestEvenObject.Value as QuestEven<ObjectT, QuestT>;
                        if ( QuestEven != null )
                        {
                            QuestEven.Object = questObject;
                            QuestEven.Callback( QuestEven );

                            iCallbackCount++;
                        }
                    }
                }
            }

            return iCallbackCount;
        }

        /// <summary>
        /// 
        /// </summary>
        public void AsyncCallback<ObjectT, QuestT>( long QuestEvenId, ObjectT QuestObject ) where QuestT : Quest
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        public void LogOut<QuestT>( long questEvenId, QuestT quest ) where QuestT : Quest
        {
            SafeDictionary<Quest, object> questDictionary = null;
            if ( m_HighestQuestEvens.TryGetValue( questEvenId, out questDictionary ) == true )
            {
                if ( questDictionary != null )
                    questDictionary.Remove( quest );
            }

            questDictionary = null;
            if ( m_NormalQuestEvens.TryGetValue( questEvenId, out questDictionary ) == true )
            {
                if ( questDictionary != null )
                    questDictionary.Remove( quest );
            }

            questDictionary = null;
            if ( m_LowestQuestEvens.TryGetValue( questEvenId, out questDictionary ) == true )
            {
                if ( questDictionary != null )
                    questDictionary.Remove( quest );
            }
        }
    }

}
#endregion